package com.sftz.modules.minapp.order.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.sftz.assets.entities.InstitutionEntity;
import com.sftz.assets.entities.TOrderEntity;
import com.sftz.assets.enums.OrderStatusEnum;
import com.sftz.assets.enums.OrderTypeEnum;
import com.sftz.framework.rersult.MobileResult;
import com.sftz.modules.minapp.order.dto.WebOrderAddDto;
import com.sftz.modules.minapp.order.form.WebOrderForm;
import com.uxuexi.core.common.util.BeanUtil;
import com.uxuexi.core.common.util.DateUtil;
import com.uxuexi.core.common.util.EnumUtil;
import com.uxuexi.core.common.util.RandomUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.web.base.service.BaseService;

/**
 * @author  赵硕
 * @Date	 2018年12月26日 	 
 */
@IocBean
public class WebOrderViewService extends BaseService<TOrderEntity> {

	private static final Log log = Logs.get();

	/**
	 * TODO(添加订单相关)
	 * @param webOrderForm 不能为空参数(userId,amountOfMoney,orderState,collegeId,collegeName,productId,productName,orderType)
	 * 
	 * status = 300 fail
	 * status = 200 success
	 * message  提示信息
	 * data     返回数据
	 */
	@Aop("txDb")
	public Object add(WebOrderForm webOrderForm) {
		//JsonResult
		Map<String, Object> map = new HashMap<String, Object>();
		if (Util.isEmpty(webOrderForm.getUserId())) {
			map = MobileResult.error("UserId为空!!", null);
			return map;
		} else if (Util.isEmpty(webOrderForm.getAmountOfMoney())) {
			map = MobileResult.error("mountOfMoney为空!!", null);
			return map;
		} else if (Util.isEmpty(webOrderForm.getProductId())) {
			map = MobileResult.error("productId为空!!", null);
			return map;
		} else if (Util.isEmpty(webOrderForm.getProductName())) {
			map = MobileResult.error("productName为空!!", null);
			return map;
		} else if (Util.isEmpty(webOrderForm.getOrderType())) {
			map = MobileResult.error("OrderType为空!!", null);
			return map;
		} else {
			//获取学院相关
			Integer collegeId = webOrderForm.getCollegeId();
			InstitutionEntity college = dbDao.fetch(InstitutionEntity.class, Cnd.where("id", "=", collegeId));

			if (!Util.isEmpty(college)) {
				webOrderForm.setCollegeName(college.getInstitutionName());
			}

			webOrderForm.setOrderState(OrderStatusEnum.WAIT_PAY.intKey());

			//检查订单类型
			OrderTypeEnum orderType = EnumUtil.get(OrderTypeEnum.class, webOrderForm.getOrderType());
			switch (orderType) {
			case COURSESERIES://专业
				webOrderForm.setOrderType(orderType.intKey());
				break;
			case COURSE://课程
				webOrderForm.setOrderType(orderType.intKey());
				break;
			default:
				map = MobileResult.error("位置的orderType数据", null);
				return map;
			}

			TOrderEntity orderEntity = new TOrderEntity();
			String tempStr = RandomUtil.randIntString(8);
			String tempM = System.currentTimeMillis() + "";
			String orderNum = tempM + tempStr;

			//复制数据
			BeanUtil.copyProperties(webOrderForm, orderEntity);
			orderEntity.setOrderNum(orderNum);

			//设置当前时间
			long currentTime = System.currentTimeMillis();
			Date date = new Date(currentTime);

			orderEntity.setOrderCreateTime(date);

			orderEntity = dbDao.insert(orderEntity);
			WebOrderAddDto orderAddDto = new WebOrderAddDto();
			BeanUtil.copyProperties(orderEntity, orderAddDto);
			orderAddDto.setCreateTime(DateUtil.Date2String(date));
			map = MobileResult.success("success", orderAddDto);
			return map;
		}
	}

}
